package com.imay.live.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.imay.base.activity.BaseActivity;
import com.imay.base.util.ViewUtil;
import com.imay.live.adapter.IndicatorFragmentAdapter;
import com.imay.live.fragment.HelpTabFragment;
import com.imay.live.fragment.NoticeChildFragment;
import com.imay.live.fragment.TestFragment;
import com.imay.live.model.HelpModel;
import com.imay.live.model.TabIndicatorModel;
import com.imay.live.proto.Result;
import com.imay.live.proto.helper.ProtoCallBack;
import com.imay.live.proto.helper.ProtoHttpFactory;
import com.imay.live.proto.helper.ProtoRequestHelper;
import com.imay.live.view.indicator.IndicatorViewPager;
import com.imay.live.view.indicator.RecyclerIndicatorView;
import com.imay.live.view.indicator.slidebar.ColorBar;
import com.imay.live.view.indicator.transition.OnTransitionTextListener;
import com.imay.live.R;

import java.util.ArrayList;
import java.util.List;

import static com.imay.live.proto.ResultCode.CodeSuccess;

/**
 * create by nick
 * 我的->设置->帮助和反馈
 */
public class HelpAndFeedbackActivity extends BaseActivity implements View.OnClickListener, ProtoCallBack {

    private RecyclerIndicatorView riv_help;
    private ViewPager vp_help;
    private String loadHelpDataCommand;

    public static void start(Context context) {
    Intent starter = new Intent(context, HelpAndFeedbackActivity.class);
    context.startActivity(starter);
}
    @Override
    protected int getLayoutId() {
        return R.layout.activity_help_and_feedback;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setTitle(getString(R.string.setting_help_and_feedback));
        riv_help = $(R.id.riv_help);
        vp_help = $(R.id.vp_help);
        $(R.id.tv_feedback_setting).setOnClickListener(this);
        loadData();
    }
    private void bindFragment(List<HelpModel> helpModels) {
        if (helpModels==null){
            return;
        }
        List<Fragment> fragmentList = new ArrayList<>(helpModels.size());
        List<TabIndicatorModel> mTabModelList = new ArrayList<>(helpModels.size());

        for (HelpModel model:helpModels) {
            mTabModelList.add(new TabIndicatorModel(model.getName(),false));
            fragmentList.add(HelpTabFragment.newInstance(model.getItems(),model.getName()));
        }
        vp_help.setOffscreenPageLimit(1);

        ColorBar colorBar =  new ColorBar(this, getResources().getColor(R.color.indicator_bar_select), getResources().getDimensionPixelOffset(R.dimen.indicator_height));
        colorBar.setWidth((int) ViewUtil.dip2px(this,25f));
        riv_help.setScrollBar(colorBar);
        riv_help.addItemDecoration(R.color.indicator_bar_unselect,R.dimen.setting_item_height);


        riv_help.setOnTransitionListener(new OnTransitionTextListener().setColorId(this, R.color.indicator_text_select, R.color.indicator_text_unselect));
        IndicatorViewPager indicatorViewPager = new IndicatorViewPager(riv_help, vp_help);
        indicatorViewPager.setAdapter(new IndicatorFragmentAdapter(this,getSupportFragmentManager(),fragmentList,mTabModelList) {
        });
    }


    private void loadData(){
        loadHelpDataCommand = ProtoRequestHelper.task(ProtoHttpFactory.getHelpList()).setTag(this.hashCode()).request(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_feedback_setting:
                FeedbackActivity.start(this);
                break;
        }
    }

    @Override
    public void onRequestSuccess(String command, String data) {
        if (command.equals(loadHelpDataCommand)){
            Result<List<HelpModel>> result = JSON.parseObject(data,new TypeReference<Result<List<HelpModel>>>(){}.getType());
            if (result.result==CodeSuccess){
                bindFragment(result.data);
            }
        }
        
    }

    @Override
    public void onRequestError(String command, int code, String message) {

    }



}
